package com.highspeed.bdk.service.physical;

import com.highspeed.bdk.bean.entity.physical.MachineBox;
import com.highspeed.bdk.repository.physical.MachineBoxRepository;
import com.highspeed.bdk.service.base.BdkBaseService;
import com.highspeed.bdk.utils.PhysicalDeviceUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by chenjiabin on 2018/3/23.
 */
@Service
@Transactional
public class MachineBoxService extends BdkBaseService<MachineBox, Integer> implements IMachineBoxService {
    private static final Log LOG = LogFactory.getLog(MachineBoxService.class);
    @Autowired
    private MachineBoxRepository machineBoxRepository;
    @Autowired
    private IPhysicalLinkService physicalLinkService;

    @Override
    public MachineBoxRepository getRepository() {
        return machineBoxRepository;
    }

    @Override
    public MachineBox save(MachineBox entity) {
        initMachineBox(entity);
        return super.save(entity);
    }


    @Override
    public MachineBox saveAndFlush(MachineBox entity) {
        initMachineBox(entity);
        return super.saveAndFlush(entity);
    }

    @Override
    public <S extends MachineBox> List<S> save(Iterable<S> entities) {
        if (entities != null) {
            for (MachineBox mb : entities) {
                initMachineBox(mb);
            }
        }
        return super.save(entities);
    }

    private void initMachineBox(MachineBox machineBox) {
        PhysicalDeviceUtil.bindParent(machineBox.getBoards(), machineBox);
    }
}
